package org.movsim.movdroid;

/*
 * This activity allows users to set the duration of traffic light phases
 * for the intersection scenario using slider bars.
 */

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;

public class TrafficLightConfig extends Activity implements SeekBar.OnSeekBarChangeListener {
	
	int red, yellow, green;
	SeekBar red1, red2, yellow1, yellow2, green1, green2;	//1 = North/South 2 = East/West
	String light1, light2;
	Button finished;
	
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.traffic_light);
		
		Toast.makeText(getApplicationContext(), "Only change the" + "\n" + 
						"North/South Lights!", Toast.LENGTH_LONG).show();
		
		Bundle bundle = this.getIntent().getExtras();
		red = bundle.getInt("red");
		yellow = bundle.getInt("yellow");
		green = bundle.getInt("green");
	
		red1 = (SeekBar) findViewById(R.id.redSeekBar1);
		red2 = (SeekBar) findViewById(R.id.redSeekBar2);
		red2.setEnabled(false);
		
		yellow1 = (SeekBar) findViewById(R.id.yellowSeekBar1);
		yellow2 = (SeekBar) findViewById(R.id.yellowSeekBar2);
		yellow2.setEnabled(false);
		
		green1 = (SeekBar) findViewById(R.id.greenSeekBar1);
		green2 = (SeekBar) findViewById(R.id.greenSeekBar2);
		green2.setEnabled(false);
		
		if (red != -1 && yellow != -1 && green != -1){
			red1.setProgress(red);
			yellow1.setProgress(yellow);
			green1.setProgress(green);
			
			red2.setProgress(green + yellow);
			yellow2.setProgress(yellow);
			green2.setProgress(red - yellow);
		}
		
		red1.setOnSeekBarChangeListener(this);
		yellow1.setOnSeekBarChangeListener(this);
		green1.setOnSeekBarChangeListener(this);
		
		finished = (Button) findViewById(R.id.button_Ok);
	
		finished.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				System.out.println(red + " " + yellow + " " + green);
				Intent data = new Intent();
				data.putExtra("red", red);
				data.putExtra("yellow", yellow);
				data.putExtra("green", green);
				setResult(RESULT_OK, data);
				finish();
			}
		});
	}


	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub
		switch (seekBar.getId()){
		case R.id.redSeekBar1:
			red = progress;
			if(yellow!=-1){
				green2.setProgress(red - yellow);
			}
			break;
		case R.id.yellowSeekBar1:
			yellow = progress;
			yellow2.setProgress(yellow);
			if(red!=-1){
				green2.setProgress(red - yellow);
			}
			if(green!=-1){
				red2.setProgress(green + yellow);
			}
			break;
		case R.id.greenSeekBar1:
			green = progress;
			if(yellow!=-1){
				red2.setProgress(green+yellow);
			}
			break;
		}
	}


	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}
}
